var search_everywhere = {
  onLoad: function() {
    // initialization code
    this.initialized = true;

    this.elt_search_everywhere_search_bar = document.getElementById("search_everywhere_search_bar");
    this.elt_search_button = document.getElementById("search_everywhere_search_button");
    this.elt_query = document.getElementById("search_everywhere_query");
    this.elt_pattern = document.getElementById("search_everywhere_pattern");
    this.elt_autofilter = document.getElementById("search_everywhere_autofilter");
    this.elt_boostexact = document.getElementById("search_everywhere_boostexact");
    this.elt_newtab = document.getElementById("search_everywhere_newtab");
    this.elt_sitebool = document.getElementById("search_everywhere_sitebool");
  },

  hideSearchEverywhereBar: function() {
      this.elt_search_everywhere_search_bar.setAttribute("collapsed", true);
  },

  showSearchEverywhereBar: function() {
      this.elt_search_everywhere_search_bar.setAttribute("collapsed", false);
      this.elt_query.focus();
  },

  toggleSearchEverywhereBar: function() {
      if ("false" == this.elt_search_everywhere_search_bar.getAttribute("collapsed")) {
          search_everywhere.hideSearchEverywhereBar();
      } else {
          search_everywhere.showSearchEverywhereBar();
      }
  },

  setCSESearchOptionsDisabled: function(disabled) {
      this.elt_pattern.disabled = disabled;
      this.elt_autofilter.disabled = disabled;
      this.elt_boostexact.disabled = disabled;
  },

  disableCSESearchOptions: function() {
      search_everywhere.setCSESearchOptionsDisabled(true);
  },

  enableCSESearchOptions: function() {
      search_everywhere.setCSESearchOptionsDisabled(false);
  },

  updateCSESearchOptionsAvailability: function(e) {
      search_everywhere.setCSESearchOptionsDisabled(1 * this.elt_sitebool.checked);
  },

  makeCSESearchUrl: function() {
      var esc_query = encodeURIComponent(this.elt_query.value);
      var esc_page_url = encodeURIComponent(gBrowser.selectedBrowser.currentURI.spec);

      // Get extraction modifiers.  Autofilter is parsed as an int, while boostexact
      // is only checked for presence/absence.
      var pattern = this.elt_pattern.selectedItem.value;
      var autofilter = 1 * this.elt_autofilter.checked;
      var boostexact_arg = "";
      if (this.elt_boostexact.checked) boostexact_arg = "&boostexact";

      var makecse_url = "www.google.com/cse/tools/makecse?url=" + esc_page_url
          + "&pattern=" + pattern
          + "&autofilter=" + autofilter
          + boostexact_arg
          + "&agent=searcheverywhereV0.4.0";

      var query_url = "http://www.google.com/cse?q=" + esc_query
          + "&cref=" + encodeURIComponent(makecse_url);

      return query_url;
  },

  makeSiteSearchUrl: function() {
      var site = gBrowser.selectedBrowser.currentURI.host;
      var sitequery = "site:" + site + " " + this.elt_query.value;
      var esc_sitequery = encodeURIComponent(sitequery);
      var query_url = "http://www.google.com/search?q=" + esc_sitequery;
      return query_url;
  },

  keyAndCommandHandler: function(e) {
      if (e.target == this.elt_search_button || (e.keyCode == 13 || e.keyCode == 10)) {
          var query_url = "";
          if (1 * this.elt_sitebool.checked) {
            query_url = search_everywhere.makeSiteSearchUrl();
          } else {
            query_url = search_everywhere.makeCSESearchUrl();
          }

          // Change url.
          var new_tab = this.elt_newtab.checked;
          if (new_tab) {
              // To add without forcing active, remove the LHS of the
              // assignment below.  What happens with tab prefs about
              // new tabs being active?
              gBrowser.selectedTab = gBrowser.addTab(query_url);
          } else {
              openUILinkIn(query_url, whereToOpenLink(e));
          }

          // Hide bar.
          search_everywhere.toggleSearchEverywhereBar();
      }
  }

};
window.addEventListener("load", function(e) { search_everywhere.onLoad(e); }, false);
